package com.campus.shop.controller.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campus.shop.common.ConstantProperties;
import com.campus.shop.common.Result;
import com.campus.shop.controller.web.vo.GoodsCartVo;
import com.campus.shop.controller.web.vo.OrderConfirmVo;
import com.campus.shop.entity.ShopCart;
import com.campus.shop.mapper.ShopCartMapper;
import com.campus.shop.service.ShopCartService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * 控制器
 *
 * @author xr
 * @since 2023-12-362 15:13:50
 */
@RestController
@RequestMapping("/web/shop-cart")
public class ShopCartController extends BaseWebController {

    @Resource
    private ShopCartService shopCartService;
    @Resource
    private ShopCartMapper shopCartMapper;

    /**
     * 添加购物车
     * @return
     */
    @PostMapping("/add-shop-cart")
    public Result addShopCart(@RequestParam("goodsId") Integer goodsId, @RequestParam("goodsNum") Integer goodsNum, @RequestParam(defaultValue = "false") Boolean isDetail) {
        QueryWrapper<ShopCart> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",goodsId);
        wrapper.eq("user_id",Integer.valueOf(getUserId()));
        wrapper.eq("status",ConstantProperties.normalStatus);
        wrapper.eq("pay_status",ConstantProperties.cartPayWaiting);
        ShopCart shopCartInfo = shopCartMapper.selectOne(wrapper);
        Integer insert;
        if(Objects.isNull(shopCartInfo)){
            Integer number = isDetail ? 1 : goodsNum;
            ShopCart shopCart = new ShopCart();
            shopCart.setGoodsId(goodsId);
            shopCart.setGoodsNum(number);
            shopCart.setUserId(Integer.valueOf(getUserId()));
            insert = shopCartMapper.insert(shopCart);
        }else{
            Integer number = isDetail ? shopCartInfo.getGoodsNum() + 1 : goodsNum;
            shopCartInfo.setGoodsNum(number);
            insert = shopCartMapper.update(shopCartInfo,wrapper);
        }
        return insert == 1 ? Result.success() : Result.error();
    }

    /**
     * 获取购物车数目
     * @return
     */
    @PostMapping("/get-cart-num")
    public Result getShopCart() {
        Integer userId = Integer.valueOf(getUserId());
        Integer cartNum = shopCartService.getCartNum(userId);
        HashMap<String, Object> res = new HashMap<>();
        res.put("cartNum",cartNum);
        return Result.success(res);
    }

    /**
     * 购物车列表
     * @return
     */
    @PostMapping("get-cart-list")
    public Result getCartList(@RequestParam("isCart") Boolean isCart){
        Integer userId = Integer.valueOf(getUserId());
        List<GoodsCartVo> list = shopCartService.getCartList(isCart,userId);
        return Result.success(list);
    }
    /**
     * 删除购物车
     */
    @PostMapping("del-shop-cart")
    public Result delShopCart(@RequestParam("cartId") Integer cartId){
        boolean isDel = shopCartService.removeById(cartId);
        return isDel ? Result.success() : Result.error();
    }

    /**
     * 修改购物车支付状态
     */
    @PostMapping("change-cart-pay-status")
    public Result changeCartPayStatus(@RequestParam("ids") String ids, @RequestParam("payStatus") Integer payStatus){
        shopCartService.changePayStatus(ids, payStatus);
        return Result.success();
    }

    /**
     * 支付 1:购物车支付 2：立即支付
     * @param ids
     * @return
     */
    @PostMapping("get-ready-goods")
    public Result getReadyGoods(@RequestParam("ids") String ids, @RequestParam("type") Integer type){
        List<OrderConfirmVo> list;
        if(type.equals(1)){
             list = shopCartService.getCartListById(Integer.valueOf(getUserId()), ids);
        }else{
             list = shopCartService.getPayGoods(Integer.valueOf(getUserId()), Integer.valueOf(ids));
        }

        return Result.success(list);
    }

}


